<?php
/**
 * Slipstream :: Highly adaptive dependency injection framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Slipstream\Configuration;

use ReflectionParameter;
use Terrafirma\Predicate\InstanceOfPredicate;
use Terrafirma\Predicate\Composite\OrPredicate;
use Terrafirma\Predicate\Type\IntPredicate;
use Terrafirma\Predicate\Type\StringPredicate;
use Slipstream\Configuration\Option\PredicatedOption;
use Slipstream\Configuration\Option\IsOptionSetPredicate;

/**
 *
 *
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class ParameterPolicy extends AbstractPolicy
{
	// {{{ Policy options
	/**
	 * @desc Name of the variable for the parameter
	 * @type string
	 * @optional Name OR position is required
	 */
	const OPTION_NAME = 'name';
	
	/**
	 * @desc Position of the parameter (starting at 0)
	 * @type int
	 * @optional Name OR position is required
	 */
	const OPTION_POSITION = 'position';
	
	/**
	 * @desc Reference that will be injected into the parameter
	 * @type ReferenceInterface
	 */
	const OPTION_REFERENCE = 'reference';
	// }}} Policy options

	/**
	 * Returns true if this policy matches another policy
	 *
	 * @param PolicyInterface $policy
	 * @return bool
	 */
	public function matches(PolicyInterface $otherPolicy)
	{
		if(!($otherPolicy instanceof $this)) {
			return false;
		}
		if($this->get(self::OPTION_NAME) === $otherPolicy->get(self::OPTION_NAME) ||
				$this->get(self::OPTION_POSITION) === $otherPolicy->get(self::OPTION_POSITION)) {
			return ($this->get(self::OPTION_DISABLED) !== $otherPolicy->get(self::OPTION_DISABLED));
		}
		return false;
	}
	
	/**
	 * Returns true if this policy matches a reflected parameter
	 *
	 * @param ReflectionParameter $parameter
	 * @return bool
	 */
	public function matchesReflection(ReflectionParameter $parameter)
	{
		if($this->get(self::OPTION_POSITION) === $parameter->getPosition()) {
			return true;
		} elseif(strnatcmp($this->get(self::OPTION_NAME), $parameter->getName()) === 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * Called in the constructor to setup the options and their default values
	 * @return void
	 */
	protected function setupOptions()
	{
		parent::setupOptions();
		
		$nameOption = new PredicatedOption(self::OPTION_NAME);
		$positionOption = new PredicatedOption(self::OPTION_POSITION);
		
		$nameOption->setPredicate(new OrPredicate(
			new StringPredicate(),
			new IsOptionSetPredicate($positionOption)));
			
		$positionOption->setPredicate(new OrPredicate(
			new IntPredicate(),
			new IsOptionSetPredicate($nameOption)));
			
		$this->addOption($nameOption);
		$this->addOption($positionOption);
		
		$this->addOption(new PredicatedOption(
			self::OPTION_REFERENCE,
			new InstanceOfPredicate('Slipstream\Configuration\Reference\ReferenceInterface')));
	}
}